package com.cd.sendRequest.controller;

import com.cd.common.config.ProjectConfig;
import com.cd.common.constant.Constants;
import com.cd.common.controller.BaseController;
import com.cd.common.domain.AjaxResult;
import com.cd.common.dto.EchartsDto;
import com.cd.common.util.AuthUtils;
import com.cd.common.util.DateUtils;
import com.cd.common.util.EnhancedImageResizer;
import com.cd.common.util.StringUtils;
import com.cd.common.util.file.FileUploadUtils;
import com.cd.sendRequest.GenericCurlExecutor;
import com.cd.sendRequest.api.SendService;
import com.cd.sendRequest.jpa.domain.DYResult;
import com.cd.sendRequest.jpa.domain.HomeMenu;
import com.cd.sendRequest.jpa.repo.DYResultRepo;
import org.apache.hc.client5.http.classic.methods.HttpGet;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.apache.hc.core5.http.io.entity.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/send")
public class SendController extends BaseController {

    private static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(SendController.class);

    @Autowired
    private SendService sendService;


    //请求接口接口 通用接口
    @PostMapping("/safe")
    public void sync(@RequestBody Map<String, String> payload,
                       @RequestParam(value = "count",defaultValue = "10")Integer count){
        GenericCurlExecutor.send(payload.get("curlCommand"),count);
    }

    @PostMapping("/startJob")
    public void startJob() throws Exception{
        sendService.executeAll();
    }

    //请求接口接口 用于从抖音拉取嘎嘎粉丝数据 每日统计
    //该接口为数据存储接口 已废弃 变更为startJob中的批量任务
    @PostMapping("/gaga-douyin")
    public void gagaDouyin() throws UnsupportedEncodingException {
        sendService.fetCurrentDouyinData();
    }


    //获取数据库内当月粉丝数量统计 传入2025为当年 2025-03为某月
    @GetMapping("/getDouyinFollowersByDate")
    public AjaxResult getDouyinFollowersByDate(@RequestParam(value = "date")String date) {
        Long currentUserId= AuthUtils.currentUserId();
        //查询当月的粉丝数据
        List<DYResult> dyResults =sendService.getDateDouyinData(date,currentUserId.toString());
        //排序 按照时间升序
        dyResults.sort((o1, o2) -> {
            Date date1 = DateUtils.parseDate(o1.getRecordTime());
            Date date2 = DateUtils.parseDate(o2.getRecordTime());
            return date1.compareTo(date2);
        });
        EchartsDto echartsDto = new EchartsDto();
        for (DYResult dyResult : dyResults) {
            echartsDto.getXData().add(dyResult.getRecordTime());
            echartsDto.getYData().add(dyResult.getFollowerCount());
        }
        //反转列表顺序方便展示
        //Collections.reverse(echartsDto.getXData());
        //Collections.reverse(echartsDto.getYData());
        //一天一条 理论十年都不会有性能瓶颈
        return success(echartsDto);
    }

    //用于拉取粉丝数据详情到数据库 查看不活跃粉丝
    @PostMapping("/getFollowerData")
    public String getFollowerData() throws Exception {
        return sendService.getAllYunFollowerData();
    }

    @PutMapping("/home-menu")
    public AjaxResult homeMenu(@RequestBody HomeMenu homeMenu) {
        sendService.addHomeMenu(homeMenu);
        return success("添加成功");
    }

    @GetMapping("/home-menu")
    public AjaxResult homeMenu() {
        List<HomeMenu> homeMenuResults =sendService.getAllHomeMenuByUserId();
        return success(homeMenuResults);
    }

    @DeleteMapping("/home-menu/{menuId}")
    public AjaxResult homeMenu(@PathVariable("menuId")Long menuId) {
        sendService.deletedHomeMenu(menuId);
        return success("删除成功");
    }

    @PostMapping("/upload/jpg")
    public AjaxResult upload(@RequestParam("file") MultipartFile file,
                             @RequestParam String type) throws Exception{
        sendService.upload(file,type);
        return success();
    }

}
